package com.cl.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;

public abstract class BaseSetupActivity extends Activity {
	//1.定义一个手势识别器
	private GestureDetector gd;
	protected SharedPreferences sp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sp = getSharedPreferences("config", MODE_PRIVATE);
		//2.实例化这个手势识别器
		gd = new GestureDetector(this, new SimpleOnGestureListener(){
			/**
			 * 当我们的手指在上面滑动的时候回调
			 */
			@Override
			public boolean onFling(MotionEvent e1, MotionEvent e2,
					float velocityX, float velocityY) {
				if((e1.getRawX() - e2.getRawX()) > 200) {
					//从右往左滑，显示下一张
					showRight();
				}
				if((e1.getRawX() - e2.getRawX()) < 200) {
					//从左往右滑，显示上一张
					showLeft();
				}
				return super.onFling(e1, e2, velocityX, velocityY);
			}

		});
	}
	protected abstract void showLeft();
	protected abstract void showRight();
	
	/**
	 * 下一步的点击事件
	 * @param view
	 */
	public void next(View view){
		showRight();
	}
	
	/**
	 *   上一步
	 * @param view
	 */
	public void pre(View view){
		showLeft();	
	}
	
	//3.使用手势识别器
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		gd.onTouchEvent(event);
		return super.onTouchEvent(event);
	}
}
